import java.awt.Color;
import java.awt.Dimension;
import java.io.IOException;
import java.util.ArrayList;

import javax.swing.*;


/**
 * This class contains the main method that runs the MULE game. It extends JFrame in order to 
 * recieve all of its functions and variables. 
 * @author Srinidhi Chaganty, Megan Beebe, Sherman Mathews, Sammy Warrayat
 */


@SuppressWarnings("serial")
public class GameController extends JFrame
{
		//the size of the frame
        public final static Dimension SIZE = new Dimension(940, 680);
        //an instance of the gameview
        private static GameView gView = new GameView();
        //an instance of the gamemodel
        private static GameModel gMod = GameModel.getInstance();
        
        /**
         * Constructor for the GameController
         * @param SIZE is the dimension screen of the Game
         * @param gView represents the GameView
         * @param gMod represents the GameMod
         */
        public GameController(){
                setPreferredSize(SIZE);
                setDefaultCloseOperation(EXIT_ON_CLOSE);
                setTitle("MULE");
                pack();
        }
        
        /**
         * The runner for the entire MULE game
         * 
         * @param args the standard parameter for main methods
         * @throws IOException 
         */
        public static void main(String [] args) throws IOException {
    		//an instance of the controller
            GameController gController = new GameController();
            /*
            ImageButton im = new ImageButton(GameController.class
                          .getResource("/PackageB/PackageBa/PackageBaa/MyImage.png"));
                          */
            gController.setVisible(true);
            ConfigurationScreen config = GameView.getInstance().getConfig();
            gController.add(config);
            gController.setVisible(true);
            while(config.configInfo[3] == null || config.configInfo[3] == (Object) false){
                    config.setVisible(true);
            }
            config.setVisible(false);
            gMod.setDifficulty((String) config.configInfo[0]);
            gMod.setTypeMap((String) config.configInfo[1]);
            gMod.setNumPlayers((Integer) config.configInfo[2]);
            gMod.setPlayerArr(new PlayerGUI[gMod.getNumPlayers()]);
            //array so that it shows the player screen the right number of times
            ArrayList<Color> colorsLeft = GameView.getInstance().getPlayerGUI().getColors();
            PlayerGUI[] playerArr = gMod.getPlayerArr();
            playerArr[0] =  GameView.getInstance().getPlayerGUI();
            gController.add(playerArr[0]);
            gMod.setPlayers(new ArrayList<Player>());
            
            for(int i = 0; i < gMod.getNumPlayers(); i++){
                    if (i > 0){
                		//creates a new PlayerGUI for the number of players picked
                        playerArr[i] = new PlayerGUI(colorsLeft);
                        gController.add(playerArr[i]);
                    }
                    playerArr[i].revalidate();
                    while(playerArr[i].getPlayerInfo()[3] == null || playerArr[i].getPlayerInfo()[3] == (Object) false){
                    	playerArr[i].setVisible(true);
                    }
                    
                    playerArr[i].setVisible(false);
                    Player nPlayer = new Player((Color)playerArr[i].getPlayerInfo()[0], (String)playerArr[i].getPlayerInfo()[1]
                                    ,(String)playerArr[i].getPlayerInfo()[2]);
                    colorsLeft.remove(playerArr[i].getPlayerInfo()[0]);
                    gMod.getPlayers().add(nPlayer);
            }
            if (gMod.getTypeMap().equalsIgnoreCase("Random")) {
            	gMod.generateMap(true);
            	gView.getInstance().setMainScreen();
            }
            else {
            	gMod.generateMap(false);
            	gView.getInstance().setMainScreen();
            }
            gMod.setRoundsLeft(gMod.getNumRounds());
            for (int i = 0; i < gMod.getNumRounds(); i++) {
                    //sorts players
                    gMod.sortPlayers(gMod.getPlayers());
                    //create a new round
                    gMod.setRound(new Round());
                    gMod.getRound().setRoundNum(i);
                    gMod.getRound().takeTurns();
                    gMod.setRoundsLeft(gMod.getRoundsLeft()-1);
                    if(i == 0) JOptionPane.showMessageDialog(gController,"GAME IS STARTING");
                    else JOptionPane.showMessageDialog(gController,"NEW ROUND IS STARTING");
                    JOptionPane.showMessageDialog(gController,"Player " + gMod.getPlayers().get(0).getName() +
                                    " goes first.");
                    //the selection phase
                    SelectionPhase sp = GameView.getInstance().getSelectionPhase();
                    //the panel for the selectionphase
                    JPanel panel = sp.generateSelectionPanel(gMod.getNumPlayers(), gMod.getPlayers(), gMod.getMap());
                    gController.add(panel);
                    panel.revalidate();
                    while ( !sp.selectionEnded()){
                            sp.setVisible(true);
                    }
                    
                    gController.remove(panel);
                    gController.validate();
                    gController.repaint();
                    
                    //TURN FOR-LOOP
                    for(int k = 0; k < gMod.getNumPlayers(); k++){
                            Player p = gMod.getPlayers().get(k);
                            gMod.setCurrPlayer(p);
                            gView.updateMain();
                            //the main screen
                            MainScreen ms = gView.getMain();
                            gController.add(ms);
                            gController.pack();
                            JOptionPane.showMessageDialog(gController,"Now starting Player " + p.getName() +
                                            "'s Turn.");
                            //creates a new turn based on the player and their turn time
                            gMod.setTurn(new Turn(p, p.getTurnTime()));
                            gMod.getTurn().getT().runTimer();
                            while (!gMod.getTurn().getT().turnEnded()){
                                    //continue;
                            }
                            gView.updateMain();
                            gController.remove(ms);
                            gController.validate();
                            gController.repaint();
                            gController.pack();
                            
                    }
                    //save?
                }
        }
}